package com.xiaobug.demo.navigation;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;

import com.ljb.commlibrary.utils.L;
import com.ljb.commlibrary.utils.T;
import com.xiaobug.baselibrary.base.BaseActivity;
import com.xiaobug.demo.R;
import com.xiaobug.demo.databinding.NavigationActivityTestBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljb on 2018/5/8.
 */

public class BottomNavigationActivity extends BaseActivity<NavigationActivityTestBinding> {
    @Override
    protected int getRootID() {
        return R.layout.navigation_activity_test;
    }

    @Override
    protected void initUI(Bundle savedInstanceState, NavigationActivityTestBinding binding) {
        initViewPager();
        BottomNavigationViewHelper.disableShiftMode(binding.bottomNavigation);
        binding.bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int index = 0;
                switch (item.getItemId()) {
                    case R.id.item_1:
                        index = 0;
                        break;
                    case R.id.item_2:
                        index = 1;
                        break;
                    case R.id.item_3:
                        index = 2;
                        break;
                    case R.id.item_4:
                        index = 3;
                        break;
                }
                mBinding.viewpager.setCurrentItem(index,false);

                CharSequence title = item.getTitle();
                T.showToast(BottomNavigationActivity.this, title.toString());

                return false;
            }
        });
        mBinding.viewpager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                L.e("选中:" + position);
                mBinding.bottomNavigation.getMenu().getItem(position).setChecked(true);
            }
        });

    }

    private void initViewPager() {
        final List<Fragment> fragmentList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            fragmentList.add(NavigationFragment.newInstance("position=" + i));
        }

        mBinding.viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState, NavigationActivityTestBinding binding) {

    }
}
